using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class fundies_controls_quotes_search : System.Web.UI.UserControl
{
    public enum Search
    {
        Quotes,
        Comments
    }

    public Search SearchMode
    {
        get
        {
            if (ViewState["SearchMode"] != null)
            {
                return (Search)ViewState["SearchMode"];
            }
            else
            {
                return Search.Quotes;
            }
        }
        set
        {
            ViewState["SearchMode"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "FSTDT Search";
    }

    protected void btnSearchQuotes_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtFundie.Text.Trim()) ||
            !string.IsNullOrEmpty(txtQuote.Text.Trim()) ||
            !string.IsNullOrEmpty(txtBoard.Text.Trim()))
        {
            CommenceSearch(Search.Quotes, 1);
        }
    }

    protected void CommenceSearch(Search mode, int page)
    {
        SearchMode = mode;
        pnlResults.Visible = true;

        int searchResults = 0;
        switch (mode)
        {
            case Search.Quotes:
                mviewResults.SetActiveView(viewQuotes);
                DAL.DataManagerPage<DAL.Quote> myQuotes = DAL.Quotes.Search(txtFundie.Text, txtQuote.Text, txtBoard.Text, page);
                searchResults = myQuotes.Pages;
                QuoteList1.SetDataSource(myQuotes.Items);
                break;
            case Search.Comments:
                mviewResults.SetActiveView(viewComments);
                DAL.DataManagerPage<DAL.Comment> myComments = DAL.Comments.Search(txtAuthor.Text, txtComment.Text, page);
                searchResults = myComments.Pages;
                Comments1.SetDataSource(myComments.Items);
                break;
        }

        if (searchResults > 0)
        {
            rptPages.DataSource = Helpers.PageNumberHelper.GetPages(searchResults, page, 20);
        }
        else
        {
            rptPages.DataSource = null;
            mviewResults.SetActiveView(viewError);
        }
        rptPages.DataBind();
    }


    protected void btnSearchComments_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtAuthor.Text.Trim()) ||
            !string.IsNullOrEmpty(txtComment.Text.Trim()))
        {
            CommenceSearch(Search.Comments, 1);
        }
    }

    protected void NewPage(object sender, CommandEventArgs e)
    {
        int page;
        if (Int32.TryParse(e.CommandArgument.ToString(), out page))
        {
            CommenceSearch(SearchMode, page);
        }
    }


}
